// MvxBaseBindingDescriptionParser.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
// 
// Project Lead - Stuart Lodge, @slodge, me@slodge.com

using System.Collections.Generic;
using System.Linq;
using T4UtilityBelt.UxDesigns.Bindings.Parser.Interfaces.Parse;
using T4UtilityBelt.UxDesigns.Bindings.Parser.Other;
//using Cirrious.MvvmCross.Binding.Interfaces.Binders;
//using Cirrious.MvvmCross.ExtensionMethods;
//using Cirrious.MvvmCross.Interfaces.Converters;

//using Cirrious.MvvmCross.Interfaces.ServiceProvider;

namespace T4UtilityBelt.UxDesigns.Bindings.Parser.Parse
{
    public static class MvxBindingDescriptionParser
        //: IMvxBindingDescriptionParser
        //, IMvxServiceConsumer
    {
        private static IMvxBindingParser CreateParser ()
		{
            return new MvxSwissBindingParser();
			//return this.GetService<IMvxBindingParser>();
		}

        //protected IMvxValueConverter FindConverter(string converterName)
        //{
        //    return null;
        //    return this.GetService<IMvxValueConverterProvider>().Find(converterName);
        //}

        #region IMvxBindingDescriptionParser Members

        public static IEnumerable<T4UtilityBelt.UxDesigns.Bindings.Binding> Parse(string text)
        {
            MvxSerializableBindingSpecification specification;
            var parser = CreateParser();
            if (!parser.TryParseBindingSpecification(text, out specification))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "Failed to parse binding specification starting with {0}",
                                      text == null ? "" : (text.Length > 20 ? text.Substring(0, 20) : text));
                return null;
            }

            if (specification == null)
                return null;

            return from item in specification
                   select SerializableBindingToBinding(item.Key, item.Value);
        }

        public static T4UtilityBelt.UxDesigns.Bindings.Binding ParseSingle(string text)
        {
            T4UtilityBelt.UxDesigns.Bindings.Binding description;
            var parser = CreateParser();
            if (!parser.TryParseBindingDescription(text, out description))
            {
                MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                      "Failed to parse binding description starting with {0}",
                                      text == null ? "" : (text.Length > 20 ? text.Substring(0, 20) : text));
                return null;
            }

            if (description == null)
                return null;

            return SerializableBindingToBinding(null, description);
        }

        public static T4UtilityBelt.UxDesigns.Bindings.Binding SerializableBindingToBinding(string targetName,
                                                                  T4UtilityBelt.UxDesigns.Bindings.Binding description)
        {
            return new T4UtilityBelt.UxDesigns.Bindings.Binding
                {
                    TargetName = targetName,
                    SourcePropertyPath = description.SourcePropertyPath,
                    Converter = description.Converter, // FindConverter(description.Converter),
                    ConverterParameter = description.ConverterParameter,
                    Mode = description.Mode,
                    FallbackValue = description.FallbackValue
                };
        }

        #endregion
    }
}